package com.tianyang.yukuaifu.business.main.fragment;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseFragment;
import com.tianyang.yukuaifu.business.auth.provider.AuthOpenProvider;
import com.tianyang.yukuaifu.business.main.bean.UserCenterOutput;
import com.tianyang.yukuaifu.business.main.contract.HomePageContract;
import com.tianyang.yukuaifu.business.main.presenter.HomePagePresenter;
import com.tianyang.yukuaifu.business.main.provider.MainOpenProvider;
import com.tianyang.yukuaifu.business.trade.provider.TradeOpenProvider;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.jump.eventbus.RefreshUserEvent;
import com.tianyang.yukuaifu.module.dialog.HintDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.OnBtnClickL;
import com.tianyang.yukuaifu.utils.ScreenUtil;
import com.tianyang.yukuaifu.utils.SystemBarHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：首页
 */

public class HomePageFragment extends BaseFragment implements HomePageContract.View {

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.bannerIv)
    ImageView bannerIv;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;

    private HomePageContract.Presenter mPresenter;

    private int verifyStatus;

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_homepage;
    }

    @Override
    protected void initView() {
        super.initView();
        int barHeight = SystemBarHelper.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) topView.getLayoutParams();
        params.height = barHeight;
        topView.setLayoutParams(params);
        setBannerHeight();
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter = new HomePagePresenter(this);
    }

    @OnClick({R.id.authRll, R.id.authManageRll, R.id.inviteCodeRll, R.id.tradeRll, R.id.merchantManageRll,
            R.id.serviceManageRll, R.id.walletRll})
    public void onClick(View view) {
        if (!isRespondEvent() || getContext() == null) return;
        if (!checkVerifyStatus()) return;
        switch (view.getId()) {
            case R.id.authRll:
                AuthOpenProvider.getInstance().openAuthSelectTypeActivity(getContext(), false);
                break;
            case R.id.authManageRll:
                AuthOpenProvider.getInstance().openMerchantInfoManageActivity(getContext());
                break;
            case R.id.inviteCodeRll:
                MainOpenProvider.getInstance().openInviteCodeActivity(getContext());
                break;
            case R.id.tradeRll:
                MainOpenProvider.getInstance().openServicePlatformActivity(getContext());
                break;
            case R.id.merchantManageRll:
                MainOpenProvider.getInstance().openMerchantManageActivity(getContext());
                break;
            case R.id.serviceManageRll:
                MainOpenProvider.getInstance().openServiceManageActivity(getContext());
                break;
            case R.id.walletRll:
                TradeOpenProvider.getInstance().openEarningsActivity(getContext());
                break;
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                EventBus.getDefault().post(new RefreshUserEvent());
                srl.finishRefresh();
            }
        });
    }

    private boolean checkVerifyStatus() {
        if (verifyStatus < 1) {
            new HintDialog(getContext())
                    .title("温馨提示")
                    .content("请先完成实名认证")
                    .showBtn("去认证")
                    .listener(new OnBtnClickL() {
                        @Override
                        public void onBtnClick() {
                            AuthOpenProvider.getInstance().openAuthServiceActivity(getContext());
                        }
                    })
                    .show();
            return false;
        }
        return true;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    /**
     * 设置banner的高度
     */
    private void setBannerHeight() {
        LinearLayout.LayoutParams topParams = (LinearLayout.LayoutParams) bannerIv.getLayoutParams();
        DisplayMetrics metric = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        int pxWidth = metric.widthPixels - ScreenUtil.dp2px(32);
        float ratio = (float) pxWidth / 686;
        float bannerHeight = 252 * ratio;
        topParams.height = (int) bannerHeight;
        bannerIv.setLayoutParams(topParams);
    }

    @Override
    public void onResume() {
        super.onResume();
        UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (output != null) {
            verifyStatus = output.getVerifyStatus();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveEntity(UserCenterOutput event) {
        if (event != null) {
            verifyStatus = event.getVerifyStatus();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public void setPresenter(HomePageContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
